<?php
namespace app\common;

use app\models\CustExternalAttr;
use yii;
use app\models\Domain;
use app\models\Region;
use app\models\ProdAttrSpecRela;
use app\models\ProdInst;
use app\models\Staff;

use app\models\AttrSpec;
use yii\helpers\Html;

class Functions
{
    //for menu/nav items
    static function formatItem($v){
        $item=[];
        if($v && is_array($v)){
            $item['url']  = isset($v['value']['url']) ? $v['value']['url'] : '#';
            $item['icon']  = isset($v['value']['icon']) && !empty($v['value']['icon'])? $v['value']['icon'] : 'fa  fa-angle-right';
            $item['label']= $v['cfg_comment'];
            if(isset($v['active']))$item['active'] = true;
        }
        return $item;
    }

    static function genMenuItems($menu_key)
    {
        $items = [];
        if( isset(Yii::$app->params[$menu_key]) && Yii::$app->params[$menu_key] && is_array(Yii::$app->params[$menu_key])){

            foreach (Yii::$app->params[$menu_key] as $k=>$v){
                if($v['cfg_pid'] == 0){
                    $items[$v['id']] = static::formatItem($v);
                }else{
                    continue;
                }
            }
            foreach (Yii::$app->params[$menu_key] as $k=>$v){
                if($v['cfg_pid']>0){
                    if(isset($items[$v['cfg_pid']])){
                        if(!isset($items[$v['cfg_pid']]['items'])){
                            $items[$v['cfg_pid']]['items']   = [];
                            $items[$v['cfg_pid']]['items'][$v['id']] = static::formatItem($v);
                        }else{
                            $items[$v['cfg_pid']]['items'][$v['id']] = static::formatItem($v);
                        }
                    }else{
                        $items[$v['id']] = static::formatItem($v); //cfg_pid 不正确的情况
                    }
                }
            }

        }
        return $items;
    }

    static public function getAttrSpecRelas($prod_spec_id)
    {
        $query = ProdAttrSpecRela::find()->alias('f');
        $query->joinWith('attrSpecs  AS g');
        $attrSpecRelas    = $query->where(['f.prod_spec_id' => $prod_spec_id ])->all();
        return $attrSpecRelas;
    }

    static public function getAttrSpecRelaValues($prod_spec_id, $attr_spec_id)
    {
        $query = ProdAttrSpecRela::find()->alias('f');
        $query->joinWith('attrSpecs  AS g');
        $attrSpecRelaValue    = $query->where(['f.prod_spec_id' => $prod_spec_id, 'f.attr_spec_id' => $attr_spec_id ])->one();
        return $attrSpecRelaValue;
    }

    static public function getAttrSpecValues( $attr_spec_id)
    {
        $query = AttrSpec::find()->alias('f');

        $attrSpecValue    = $query->where([ 'f.attr_spec_id' => $attr_spec_id ])->one();
        return $attrSpecValue;
    }


    static public function getAttrSpecDownList($attrSpecRelas)
    {
        $attrSpecDownList = [];

        foreach ($attrSpecRelas as $attrSpecRela){

            $attrSpecDownList[$attrSpecRela->attr_spec_id] = $attrSpecRela->attrSpecs->attr_spec_name;
        }

        return $attrSpecDownList;
    }



    static public function getAllDomains()
    {
        $query = Domain::find();
        $domains = $query->all();

        foreach ($domains as   $values)
        {
            $domainOut[$values->domain_table_name][$values->domain_field_name][$values->domain_code] = $values->domain_name;
        }

        return $domainOut;
    }

    static public function getDomains($domainTableName = null, $domainFieldName = null)
    {
        $where     = []; // 查询条件
        $domainOut = []; //返回的domain数组，以code为索引，name为值的数组
        $where['domain_table_name'] = $domainTableName;
        $where['domain_field_name'] = $domainFieldName;
        $query = Domain::find();
        $domains = $query->where($where)->all();


        foreach ($domains as   $values)
        {
            $domainOut[$values->domain_code ] = $values->domain_name;
        }

        return $domainOut;
    }


    static public function getDomainsName($domainTableName = null, $domainFieldName = null, $domain_code  = null)
    {
        $where     = []; // 查询条件
        $domainOut = []; //返回的domain数组，以code为索引，name为值的数组
        $where['domain_table_name'] = $domainTableName;
        $where['domain_field_name'] = $domainFieldName;
        $where['domain_code'] = $domain_code;
        $query = Domain::find();
        $domains = $query->where($where)->one();




        return $domains->domain_name;
    }

    //获取名字 号码
    static public function getCustExternalAttrsName($cust_id = null, $attr_spec_id = null)
    {
        $where     = []; // 查询条件
        $domainOut = []; //返回的domain数组，以code为索引，name为值的数组
        $where['cust_id'] = $cust_id;
        $where['attr_spec_id'] = $attr_spec_id;

        $query = CustExternalAttr::find();
        $domains = $query->where($where)->one();

        return $domains->content;
    }


    // 取出区域编码对应的文字
    static public function getRegions()
    {

        $regions = [];
        $query = Region::find()->alias('a');
        $queryRegions = $query->all();
        foreach ($queryRegions as $queryRegion)
        {
            $regions[$queryRegion->id] = $queryRegion->region_name;
        }

        return $regions;
    }

    //  返回对应的下拉按钮列表文本
    static public function getActionDropdownContext($btnBegin, $btns)
    {
        $actionDropdownContext = '';
        $begin = '<div class="btn-group">';
        $end = '</div>';
        $btnEnd = '<button type="button" class="btn btn-warning btn-xs dropdown-toggle" data-toggle="dropdown">
                    <span class="caret"></span>
                    <span class="sr-only">Toggle Dropdown</span>
                  </button>';

        $actionDropdownContext = $begin . $btnBegin . $btnEnd ;
        $actionDropdownContext .= '<ul class="dropdown-menu" role="menu">';
        foreach ($btns as $btn)
        {
            $actionDropdownContext .= '<li>';
            $actionDropdownContext .= $btn;
            $actionDropdownContext .= '</li>';
        }
        $actionDropdownContext .= '</ul></div>';
        return $actionDropdownContext;
    }


    // 使用膜版进行二次加工
    static public function getFormatColumns($customs, $columns,$searchModel=null)
    {
        $ii = 0;
        $columns2 = [];
        foreach ($customs as $custom) {
            if (!$columns[$custom->attr_field_name]) {
                continue;
            }

            if ($custom->hide_flag!=1)
            {
                continue;
            }
          //  $columns[$custom->attr_field_name]['label'] = $custom->show_name;

            $columns2[$ii] = $columns[$custom->attr_field_name];
            if ($custom->attr_field_name!='uid')
            {
                $columns2[$ii]['label'] = $custom->show_name;

                if ($searchModel)
                {
                    if (!$columns2[$ii]['filter'] && $custom->custom_type!=2)
                    {
                        // 如果类型为3 代表为domain型
                        if($custom->attrSpecs->attr_spec_type_id==3)
                        {
                            $dropDownList =  Functions::getDomains('attr_spec', $custom->attr_spec_id);
                            $dropDownList[''] = '全部';
                            ksort($dropDownList);

                            $columns2[$ii]['filter'] = Html::activeDropDownList($searchModel, $custom->attr_field_name, $dropDownList, []);
                        }
                        else
                        {
                            $columns2[$ii]['filter'] = Html::activeTextInput($searchModel, $custom->attr_field_name, ['class'=>'form-control']);
                        }

                    }

                }
            }



//            if ($columns2[$ii]['label'] =='需求面积')
//            {
//                $columns2[$ii]['filter'] = Html::activeTextInput($searchModel, 'need_area', ['class'=>'form-control']);//,'style'=>'height:33px'
//            }

            $columns2[$ii]['options'] = ['style' => 'width:' . $custom->attr_spec_width . 'px'];
            $ii++;
        }

        return $columns2;
    }

    static public function getWeekend(  ){//判断前4天时间是否包含周末

        //$Weekend=date('w');//0就是周日 6是周六
        //date('w',strtotime(日期字符串));
        $datetime = new \DateTime();
        $interval = \DateInterval::createFromDateString('-1 day');
        $period = new \DatePeriod($datetime, $interval, 4);
        foreach ($period as $date) {
            $day_time = $date->format('Y-m-d');
            $Weekend=date('w',strtotime($day_time));
            if($Weekend==0){
                $day=2;
                break;
            }elseif($Weekend==6){
                $day=1;
            }else{
                $day=0;
            }

        }


        return $day;
    }

    //判断自己的分组
    static public function getMyid(  ){
        // 取出自己的u_id
        $user_id = Yii::$app->user->id;

        //取客户出对应的组
        $staffQuery = Staff::find()->alias('s');
        $staffModel = $staffQuery->where(['s.user_id' => $user_id ])->one();

        $id = $staffModel->staff_group;
        return $id;
    }


     static public  function getNeedBetween($kw1,$mark1,$mark2){
        $kw=$kw1;
        $kw='123'.$kw.'123';
        $st =stripos($kw,$mark1);
        $ed =stripos($kw,$mark2);
        if(($st==false||$ed==false)||$st>=$ed)
            return 0;
        $kw=substr($kw,($st+1),($ed-$st-1));
        return $kw;
        }

}